﻿package com.sh.game.display
{
    import __AS3__.vec.*;
    import com.core.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.system.*;
    import morn.core.handlers.*;

    public class XMLMovieClip extends Sprite
    {
        protected var _bitmap:Bitmap;
        protected var _autoPlay:Boolean;
        protected var _autoStopAtRemoved:Boolean = true;
        protected var _interval:int;
        protected var _from:int = -1;
        protected var _to:int = -1;
        protected var _complete:Handler;
        protected var _isPlaying:Boolean;
        protected var _toolTip:Object;
        private var bmds:XMLClipData;
        private var _color:ColorTransform;
        private var _autoCut:Boolean = true;
        private var _frameXY:Array;
        private var _domain:ApplicationDomain;
        private var mPivotX:Number = 0;
        private var mPivotY:Number = 0;
        private var _index:int = 0;
        public var _xml:XML;
        public var _clsurl:Object;
        private var _clips:Vector.<BitmapData>;

        public function XMLMovieClip(param1, param2:XML, param3:ColorTransform = null, param4:Boolean = true, param5:ApplicationDomain = null)
        {
            this._interval = MornConfig.MOVIE_INTERVAL;
            this._clsurl = param1;
            this._xml = param2;
            this._autoCut = param4;
            this._color = param3;
            this._domain = param5;
            this._autoPlay = true;
            this._bitmap = new Bitmap();
            this.addChild(this._bitmap);
            this._autoPlay = true;
            this.initBitmaps();
            this.initialize();
            return;
        }// end function

        protected function initialize() : void
        {
            addEventListener(Event.ADDED_TO_STAGE, this.onAddedToStage);
            addEventListener(Event.REMOVED_FROM_STAGE, this.onRemovedFromStage);
            return;
        }// end function

        protected function onAddedToStage(event:Event) : void
        {
            if (this._autoPlay)
            {
                this.play();
            }
            return;
        }// end function

        protected function onRemovedFromStage(event:Event) : void
        {
            if (this._autoStopAtRemoved)
            {
                this.stop();
            }
            return;
        }// end function

        public function set scale(param1:Number) : void
        {
            var _loc_2:* = param1;
            scaleY = param1;
            scaleX = _loc_2;
            return;
        }// end function

        public function get frame() : int
        {
            return this._index;
        }// end function

        public function get toolTip() : Object
        {
            return this._toolTip;
        }// end function

        public function set toolTip(param1:Object) : void
        {
            if (this._toolTip != param1)
            {
                this._toolTip = param1;
                if (Boolean(param1))
                {
                    addEventListener(MouseEvent.ROLL_OVER, this.onRollMouse);
                    addEventListener(MouseEvent.ROLL_OUT, this.onRollMouse);
                }
                else
                {
                    removeEventListener(MouseEvent.ROLL_OVER, this.onRollMouse);
                    removeEventListener(MouseEvent.ROLL_OUT, this.onRollMouse);
                }
            }
            return;
        }// end function

        protected function onRollMouse(event:MouseEvent) : void
        {
            if (event.type == MouseEvent.ROLL_OVER)
            {
                ModelProxy.change("tips", "normal", {data:this._toolTip});
            }
            else
            {
                ModelProxy.change("tips", "state", "close");
            }
            return;
        }// end function

        public function get totalFrame() : int
        {
            return this.clips ? (this.clips.length) : (0);
        }// end function

        public function get autoPlay() : Boolean
        {
            return this._autoPlay;
        }// end function

        public function set autoPlay(param1:Boolean) : void
        {
            if (this._autoPlay != param1)
            {
                this._autoPlay = param1;
                if (this._autoPlay)
                {
                    this.play();
                }
                else
                {
                    this.stop();
                }
            }
            return;
        }// end function

        public function get interval() : int
        {
            return this._interval;
        }// end function

        public function set interval(param1:int) : void
        {
            if (this._interval != param1)
            {
                this._interval = param1;
                if (this._isPlaying)
                {
                    this.play();
                }
            }
            return;
        }// end function

        public function get isPlaying() : Boolean
        {
            return this._isPlaying;
        }// end function

        public function set isPlaying(param1:Boolean) : void
        {
            this._isPlaying = param1;
            return;
        }// end function

        public function play() : void
        {
            this._isPlaying = true;
            this.frame = this._index;
            App.timer.doLoop(this._interval, this.loop);
            return;
        }// end function

        protected function loop() : void
        {
            var _loc_1:String = this;
            var _loc_2:* = this.frame + 1;
            _loc_1.frame = _loc_2;
            return;
        }// end function

        public function stop() : void
        {
            App.timer.clearTimer(this.loop);
            this._isPlaying = false;
            return;
        }// end function

        public function gotoAndPlay(param1:int) : void
        {
            this.frame = param1;
            this.play();
            return;
        }// end function

        public function gotoAndStop(param1:int) : void
        {
            this.stop();
            this.frame = param1;
            return;
        }// end function

        public function playFromTo(param1:int = -1, param2:int = -1, param3:Handler = null) : void
        {
            this._from = param1 == -1 ? (0) : (param1);
            this._to = param2 == -1 ? (-1) : (param2);
            this._complete = param3;
            this.gotoAndPlay(this._from);
            return;
        }// end function

        public function get smoothing() : Boolean
        {
            return this._bitmap.smoothing;
        }// end function

        public function set smoothing(param1:Boolean) : void
        {
            this._bitmap.smoothing = param1;
            return;
        }// end function

        public function get bitmap() : Bitmap
        {
            return this._bitmap;
        }// end function

        public function set frame(param1:int) : void
        {
            var _loc_2:Handler = null;
            var _loc_3:Array = null;
            if (this.clips)
            {
                this._index = param1 < this.clips.length && param1 > -1 ? (param1) : (0);
                if (this._index == this._to)
                {
                    this.stop();
                    this._to = -1;
                    if (this._complete != null)
                    {
                        _loc_2 = this._complete;
                        this._complete = null;
                        _loc_2.execute();
                    }
                }
                if (this._autoCut)
                {
                    _loc_3 = this._frameXY[this.frame];
                    this._bitmap.bitmapData = this.clips[this.frame];
                    if (_loc_3)
                    {
                        if (this._bitmap)
                        {
                            this._bitmap.x = -_loc_3[0] - this.mPivotX;
                            this._bitmap.y = -_loc_3[1] - this.mPivotY;
                        }
                    }
                }
            }
            else if (this._complete != null)
            {
                _loc_2 = this._complete;
                this._complete = null;
                _loc_2.execute();
            }
            return;
        }// end function

        public function get pivotY() : Number
        {
            return this.mPivotY;
        }// end function

        public function set pivotY(param1:Number) : void
        {
            if (this.mPivotY != param1)
            {
                this.mPivotY = param1;
                if (this._bitmap)
                {
                    this.frame = this.frame;
                }
            }
            return;
        }// end function

        public function get pivotX() : Number
        {
            return this.mPivotX;
        }// end function

        public function set pivotX(param1:Number) : void
        {
            if (this.mPivotX != param1)
            {
                this.mPivotX = param1;
                if (this._bitmap)
                {
                    this.frame = this.frame;
                }
            }
            return;
        }// end function

        private function initBitmaps() : void
        {
            var _loc_1:BitmapData = null;
            var _loc_2:Class = null;
            if (this._clsurl is String)
            {
                if (!this._domain)
                {
                    this.bmds = App.asset.getXMLClips(this._clsurl, this._xml, this._autoCut);
                }
                else
                {
                    if (!this._domain.hasDefinition(this._clsurl))
                    {
                        throw new Error("缺少资源" + this._clsurl);
                    }
                    _loc_2 = this._domain.getDefinition(this._clsurl) as Class;
                    _loc_1 = new _loc_2 as BitmapData;
                    this.bmds = new XMLClipData(_loc_1, this._xml, this._autoCut);
                    _loc_1.dispose();
                }
            }
            else if (this._clsurl is BitmapData)
            {
                _loc_1 = this._clsurl;
                this.bmds = new XMLClipData(_loc_1, this._xml, this._autoCut);
            }
            else if (this._clsurl is XMLClipData)
            {
                this.bmds = this._clsurl;
            }
            else
            {
                this.bmds = new XMLClipData(this._clsurl, this._xml, this._autoCut);
            }
            if (this.bmds == null)
            {
                return;
            }
            this._frameXY = this.bmds.data;
            this.clips = this.bmds.bitmapDatas;
            return;
        }// end function

        public function set frameInfos(param1:Array) : void
        {
            this._frameXY = param1;
            return;
        }// end function

        public function get clips() : Vector.<BitmapData>
        {
            return this._clips;
        }// end function

        public function set clips(param1:Vector.<BitmapData>) : void
        {
            if (param1)
            {
                this._clips = param1;
            }
            return;
        }// end function

        public function dispose(param1:Boolean = false) : void
        {
            this._bitmap.bitmapData = null;
            App.asset.destroyClips(this._clsurl);
            if (param1)
            {
                App.loader.clearResLoaded(this._clsurl);
            }
            this._xml = null;
            if (this.bmds)
            {
                if (param1)
                {
                    if (this._clsurl is String && !this._domain)
                    {
                        App.asset.destroyXMLClips(this._clsurl);
                    }
                    else
                    {
                        this.bmds.dispose();
                    }
                }
                this.bmds = null;
            }
            this._clsurl = null;
            if (this._color)
            {
                this._color = null;
            }
            this._autoCut = false;
            this._domain = null;
            if (this._frameXY)
            {
                this._frameXY = null;
            }
            return;
        }// end function

    }
}
